﻿#include "systemmonitormanager.h"
#include "syscfg.h"
#include "dbservice.h"
#include "alarmreq.h"
#include "TcpConnectHandler.h"

SystemMonitorManager gSystemMonitorManger;


SystemMonitorManager::SystemMonitorManager()
{

}

void SystemMonitorManager::startAllTask()
{
    cpuMonitor = new SysCPUMonitor(this);
    cpuMonitor->run();
    memMointor = new SysMEMMonitor(this);
    memMointor->run();
    diskMonitor = new SysDiskMonitor(this);
    diskMonitor->run();
}

void SystemMonitorManager::stopAllTask()
{
    if(cpuMonitor)
    {
        cpuMonitor->stopAndWaitThreadQuit();
        SafeDeleteObj(cpuMonitor);
    }

    if(memMointor)
    {
        memMointor->stopAndWaitThreadQuit();
        SafeDeleteObj(memMointor);
    }

    if(diskMonitor)
    {
        diskMonitor->stopAndWaitThreadQuit();
        SafeDeleteObj(diskMonitor);
    }
}

void SystemMonitorManager::updateCurrentDiskUsage(list<DiskInfo> &usage)
{
    lastDiskInfo = usage;
    foreach (DiskInfo diskInfo, usage) {
        if((1-diskInfo.freeMB/diskInfo.totalMB )*100 > DISK_ALARM_THRESHOLD)
        {
            SysMonitorLog log(SYS_MONITOR_LOG_KIND_DISK,diskInfo.disk +"使用率：" + float2QStr((1-diskInfo.freeMB/diskInfo.totalMB )*100) +
                              "%，已超过阀值：" + int2QStr(DISK_ALARM_THRESHOLD) + "%",sayTime(QDateTime::currentDateTime()));

            QString faileResult;

           AlarmReq req(AlarmReqType_Memory,ALARM_REQ_MODE_MEM,  log.action.toStdString());
           if(sendMsgWithCommonResp(&req, WATCHDOG_SERVER_IP, WATCHDOG_SERVER_PORT, faileResult) == f_success)
           {
               log.isSuccess = true;
           }
           else
           {
               log.isSuccess = false;
               log.failReason = faileResult;
           }

           if(f_success == gDBService.saveOneSysMonitorLog(log))
           {
               emit this->onSysMonitorAlarmFired();
           }
        }
    }
    emit this->diskUsageUpdated();
}

void SystemMonitorManager::updateCurrentMemUsage(MEMORYSTATUSEX memStatus)
{
    lastMEMUsages.push_back(memStatus);
    if(lastMEMUsages.size() > MEM_USAGE_MAX_DISPLAY_COUNT)
    {
        lastMEMUsages.erase(lastMEMUsages.begin());
    }
    if(memStatus.dwMemoryLoad > MEM_ALARM_THRESHOLD)
    {
        SysMonitorLog log(SYS_MONITOR_LOG_KIND_MEMORY,"内存使用率：" + float2QStr(memStatus.dwMemoryLoad) +
                          "%，已超过阀值：" + int2QStr(MEM_ALARM_THRESHOLD) + "%",sayTime(QDateTime::currentDateTime()));

        QString faileResult;

       AlarmReq req(AlarmReqType_Memory,ALARM_REQ_MODE_MEM,  log.action.toStdString());
       if(sendMsgWithCommonResp(&req, WATCHDOG_SERVER_IP, WATCHDOG_SERVER_PORT, faileResult) == f_success)
       {
           log.isSuccess = true;
       }
       else
       {
           log.isSuccess = false;
           log.failReason = faileResult;
       }

       if(f_success == gDBService.saveOneSysMonitorLog(log))
       {
           emit this->onSysMonitorAlarmFired();
       }
    }

    emit this->memUsageUpdated();
}

void SystemMonitorManager::updateCurrentCpuUsage(double cpuUsage)
{
    lastCPUUsages.push_back(cpuUsage);
    if(lastCPUUsages.size() > CPU_USAGE_MAX_DISPLAY_COUNT)
    {
        lastCPUUsages.erase(lastCPUUsages.begin());
    }
    if(cpuUsage >= CPU_ALARM_THRESHOLD)
    {
        SysMonitorLog log(SYS_MONITOR_LOG_KIND_CPU,"CPU使用率：" + float2QStr(cpuUsage) +
                          "%，已超过阀值：" + int2QStr(CPU_ALARM_THRESHOLD) + "%",sayTime(QDateTime::currentDateTime()));

        QString faileResult;

       AlarmReq req(AlarmReqType_CPU,ALARM_REQ_MODE_CPU,  log.action.toStdString());
       if(sendMsgWithCommonResp(&req, WATCHDOG_SERVER_IP, WATCHDOG_SERVER_PORT, faileResult) == f_success)
       {
           log.isSuccess = true;
       }
       else
       {
           log.isSuccess = false;
           log.failReason = faileResult;
       }

       if(f_success == gDBService.saveOneSysMonitorLog(log))
       {
           emit this->onSysMonitorAlarmFired();
       }
    }
    emit this->cpuUsageUpdated();
}




void SystemMonitorManager::restartCPUTask()
{
    if(cpuMonitor)
    {
        cpuMonitor->stopAndWaitThreadQuit();
        SafeDeleteObj(cpuMonitor);
        cpuMonitor = new SysCPUMonitor(this);
        cpuMonitor->run();
    }
}
void SystemMonitorManager::restartDiskTask()
{
    if(diskMonitor)
    {
        diskMonitor->stopAndWaitThreadQuit();
        SafeDeleteObj(diskMonitor);
        diskMonitor = new SysDiskMonitor(this);
        diskMonitor->run();
    }
}

void SystemMonitorManager::restartMemoryTask()
{
    if(memMointor)
    {
        memMointor->stopAndWaitThreadQuit();
        SafeDeleteObj(memMointor);
        memMointor = new SysMEMMonitor(this);
        memMointor->run();
    }
}
